home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / music / cthugha5.zip / CTHU5SRC.ZIP / PETE.C < prev    next >
C/C++ Source or Header  |  1994-08-19  |  7KB  |  314 lines

  1. //
  2. // Cthugha - Audio Seeded Image Processing
  3. //
  4. // Zaph, Digital Aasvogel Group, Torps Productions 1993-1994
  5. //
  6.  
  7.  
  8. // Why PETE.C ??
  9. // 'cause late one Saturday night, Pete wanted to write some code
  10. // and I let him do a couple of functions (only one survives, I 
  11. // took over the others :-)
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <dos.h>
  16. #include <io.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include <conio.h>
  21.  
  22. #include "cthugha.h"
  23. #include "charset.h"
  24. #include "pete.h"
  25.  
  26. /* Defines for Soundblaster and Soundblaster Pro IO address */
  27.  
  28. int sine[320];
  29.  
  30.  
  31. // #define N 16 /* number of FFT terms, do not change this!!! See also LOG2N */
  32. #define N 64 /* number of FFT terms, do not change this!!! See also LOG2N */
  33. /*----------------------------------- FFT ------------------------------------*/
  34.  
  35. #define S0  0 /* corresponds to 0.0 */
  36. #define S1  3
  37. #define S2  6
  38. #define S3  9 /* sin() table with 4 bit precision */
  39. #define S4 11 /* increasing precision will cause short integer overflows... */
  40. #define S5 13 /* too bad, I really would have liked more precision */
  41. #define S6 15
  42. #define S7 16
  43. #define S8 16 /* corresponds to 1.0 */
  44. // #define SCALE 11 /* scale FFT with 2**SCALE from big values to low */
  45. #define SCALE 7 /* scale FFT with 2**SCALE from big values to low */
  46.  
  47. static int s[N+N],c[N+N];
  48. static int FFTenable=FALSE; /* Maximum sampling rate for initial SW tuning */
  49. static int waitfactor=3; /* Best initial sampling rate for my 12MHz 286 */
  50. static int fft[N],oldfft[N];
  51.  
  52. #include <math.h>
  53. #define CTEPI (3.14159265358979323846/(float)N)
  54. #define TRIG  16 /* trig table lookup precision compatible with S? way above */
  55.  
  56. void init_pete(void)
  57. {
  58.     int i;
  59.   static float xx,ss,cc;
  60.   static int k;
  61.  
  62.     for (i=0; i<320; i++)
  63.         sine[i]=(int)(128*sin((float)i*0.03927));
  64.  
  65.  
  66.   for (k=0 ; k<(N+N) ; k++) {
  67.     xx=CTEPI*k;
  68.     ss=TRIG*sin(xx);
  69.     cc=TRIG*cos(xx);
  70.     if (ss>0.0) ss+=0.5; else ss-=0.5;
  71.     if (cc>0.0) cc+=0.5; else cc-=0.5;
  72.     s[k]=(int)ss; /* truncate */
  73.     c[k]=(int)cc; /* truncate */
  74.   }
  75.  
  76.  
  77. }
  78. void pete_0(void)
  79. {
  80.     int temp,temp2,x;
  81.     static int xoff0=BUFF_WIDTH/2,yoff0=BUFF_HEIGHT/2;
  82.     static int xoff1=BUFF_WIDTH/2,yoff1=BUFF_HEIGHT/2;
  83.  
  84.     xoff0 += stereo[0][0]%9-4;
  85.     yoff0 += stereo[1][0]%9-4;
  86.  
  87.     xoff1 += stereo[0][1]%9-4;
  88.     yoff1 += stereo[1][1]%9-4;
  89.  
  90.     if (xoff0<0)
  91.         xoff0+=BUFF_WIDTH;
  92.     if (yoff0<0)
  93.         yoff0+=BUFF_BOTTOM;
  94.  
  95.     if (xoff1<0)
  96.         xoff1+=BUFF_WIDTH;
  97.     if (yoff1<0)
  98.         yoff1+=BUFF_BOTTOM;
  99.  
  100.     xoff0=xoff0%BUFF_WIDTH;
  101.     xoff1=xoff1%BUFF_WIDTH;
  102.  
  103.     yoff0=yoff0%BUFF_HEIGHT;
  104.     yoff1=yoff1%BUFF_HEIGHT;
  105.  
  106.     for (x=0; x<BUFF_WIDTH; x++) {
  107.         temp=stereo[x][0];
  108.         temp2=stereo[(x+80)%BUFF_WIDTH][0];
  109.  
  110.         buff[((temp>>2)+yoff0)%BUFF_BOTTOM][((temp2>>2)+xoff0)%BUFF_WIDTH]=table[curtable][temp];
  111.  
  112.         temp=stereo[x][1];
  113.         temp2=stereo[(x+80)%BUFF_WIDTH][1];
  114.  
  115.         buff[((temp>>2)+yoff1)%BUFF_BOTTOM][((temp2>>2)+xoff1)%BUFF_WIDTH]=table[curtable][temp];
  116.  
  117.     }
  118.  
  119. }
  120.  
  121. void pete_1(void)
  122. {
  123.     int temp,x,left=0,right=0;
  124.  
  125.     for (x=0; x<BUFF_WIDTH; x++) {
  126.         left += abs(stereo [x][0] - 128);
  127.         right += abs(stereo [x][1] - 128);
  128.     }
  129.  
  130.     left = left / (160);
  131.     right = right / (160);
  132.  
  133.     left=min(left,199);
  134.     right=min(right,199);
  135.  
  136.     for (x=0; x<160; x++) {
  137.         temp=stereo[x][0];
  138.         buff[200-(abs((left)*sine[x])>>8)][(x)]=table[curtable][temp];
  139.     }
  140.     for    (x=160; x<320; x++) {
  141.         temp=stereo[x][1];
  142.         buff[200-(abs((right)*sine[x])>>8)][(x)]=table[curtable][temp];
  143.     }
  144.  
  145. }
  146.  
  147. void pete_2(void)
  148. {
  149.     int temp,x;
  150.  
  151.     for (x=0; x<200; x++) {
  152.         temp=stereo[x][0];
  153.         buff[x][160-(temp>>2)]=table[curtable][sine[temp]];
  154.         temp=stereo[x][1];
  155.         buff[x][160+(temp>>2)]=table[curtable][sine[temp]];
  156.     }
  157.  
  158. }
  159.  
  160.  
  161. void moles_fract(void)
  162. {
  163.     int temp,x;
  164.     static int xoff0=BUFF_WIDTH/2,yoff0=BUFF_HEIGHT/2;
  165.     static int xoff1=BUFF_WIDTH/2,yoff1=BUFF_HEIGHT/2;
  166.  
  167.     temp=stereo[0][0];
  168.     for (x=0; x<BUFF_WIDTH-2; x+=2) {
  169.         xoff0 += (stereo[x][0]-temp)>>1;
  170.         temp=stereo[x][0];
  171.  
  172.  
  173.         if (xoff0<0)
  174.             xoff0=BUFF_WIDTH-1;
  175.  
  176.         xoff0=xoff0%BUFF_WIDTH;
  177.  
  178.         buff[yoff0][xoff0]=table[curtable][temp];
  179.  
  180.         yoff0 += (stereo[x+1][0]-temp)>>1;
  181.         temp=stereo[x+1][0];
  182.  
  183.         if (yoff0<0)
  184.             yoff0=BUFF_HEIGHT-1;
  185.  
  186.         yoff0=yoff0%BUFF_HEIGHT;
  187.  
  188.         buff[yoff0][xoff0]=table[curtable][temp];
  189.  
  190.     }
  191.  
  192.     temp=stereo[0][1];
  193.     for (x=0; x<BUFF_WIDTH-2; x+=2) {
  194.         xoff1 += (stereo[x][1]-temp)>>1;
  195.         temp=stereo[x][1];
  196.  
  197.  
  198.         if (xoff1<0)
  199.             xoff1=BUFF_WIDTH-1;
  200.  
  201.         xoff1=xoff1%BUFF_WIDTH;
  202.  
  203.         buff[yoff1][xoff1]=table[curtable][temp];
  204.  
  205.         yoff1 -= (stereo[x+1][1]-temp)>>1;
  206.         temp=stereo[x+1][1];
  207.  
  208.         if (yoff1<0)
  209.             yoff1=BUFF_HEIGHT-1;
  210.  
  211.         yoff1=yoff1%BUFF_HEIGHT;
  212.  
  213.         buff[yoff1][xoff1]=table[curtable][temp];
  214.  
  215.     }
  216. }
  217.  
  218. void moles_fract2(void)
  219. {
  220.     int temp,x;
  221.     static int xoff0=BUFF_WIDTH/2,yoff0=BUFF_HEIGHT/2;
  222.     static int xoff1=BUFF_WIDTH/2,yoff1=BUFF_HEIGHT/2;
  223.  
  224.     temp=stereo[0][0];
  225.     for (x=0; x<BUFF_WIDTH-2; x+=2) {
  226.         xoff0 += (stereo[x][0]-temp);
  227.         temp=stereo[x][0];
  228.  
  229.  
  230.         if (xoff0<0)
  231.             xoff0=BUFF_WIDTH-1;
  232.  
  233.         xoff0=xoff0%BUFF_WIDTH;
  234.  
  235.         buff[yoff0][xoff0]=table[curtable][temp];
  236.  
  237.         yoff0 += (stereo[x+1][0]-temp);
  238.         temp=stereo[x+1][0];
  239.  
  240.         if (yoff0<0)
  241.             yoff0=BUFF_HEIGHT-1;
  242.  
  243.         yoff0=yoff0%BUFF_HEIGHT;
  244.  
  245.         buff[yoff0][xoff0]=table[curtable][temp];
  246.  
  247.     }
  248.  
  249.     temp=stereo[0][1];
  250.     for (x=0; x<BUFF_WIDTH-2; x+=2) {
  251.         xoff1 += (stereo[x][1]-temp);
  252.         temp=stereo[x][1];
  253.  
  254.  
  255.         if (xoff1<0)
  256.             xoff1=BUFF_WIDTH-1;
  257.  
  258.         xoff1=xoff1%BUFF_WIDTH;
  259.  
  260.         buff[yoff1][xoff1]=table[curtable][temp];
  261.  
  262.         yoff1 -= (stereo[x+1][1]-temp);
  263.         temp=stereo[x+1][1];
  264.  
  265.         if (yoff1<0)
  266.             yoff1=BUFF_HEIGHT-1;
  267.  
  268.         yoff1=yoff1%BUFF_HEIGHT;
  269.  
  270.         buff[yoff1][xoff1]=table[curtable][temp];
  271.  
  272.     }
  273. }
  274.  
  275.  
  276. void FFT(int channel)
  277. { /* compute fast short integer FT */
  278.     static int data,kl,a[N],b[N],fff[N];
  279.     static int k,l,f[N+N];
  280.     static int level,oldlevel,line,column;
  281.  
  282.     for (k=0 ; k<(N+N) ; k++) {
  283.         f[k]=stereo[k*2][channel]-128;
  284.     }
  285.  
  286.     for (l=1 ; l<N ; l++)
  287.         a[l]=b[l]=0;
  288.     for (k=0 ; k<(N+N) ; k++) 
  289.         for (l=1 ; l<N ; l++) {
  290.             kl=(l*k)%(N+N);
  291.             a[l]+=f[k]*c[kl];
  292.             b[l]+=f[k]*s[kl];
  293.         }
  294.     for (l=1 ; l<N ; l++) {
  295.         fff[l]=(abs(a[l])>>SCALE)+(abs(b[l])>>SCALE);
  296.     }
  297.  
  298.     for (k=N*4-3; k<BUFF_WIDTH; k++)  // Clean stuff past the end of the fft
  299.         stereo[k][channel]=128;
  300.     stereo[0][channel]=128;
  301.  
  302.     for (l=1; l<N ; l++) {
  303.         level=fff[l];
  304.  
  305.         if (level>127) { // Expand the loops!!
  306.             for (k=0; k<4; k++)
  307.                 stereo[l*4+k][channel]=255;
  308.         } else {
  309.             for (k=0; k<4; k++)
  310.                 stereo[l*4+k][channel]=level+128;
  311.         }
  312.   }
  313. }
  314.